exports.login = function (req, res) {


    //最终返回结果
    var resultData = {};

    //用户信息
    var userData = null;

    //节点所拥有的角色
    var organizationRoleArr = [];

    //用户所拥有的角色
    var userRoleArr = [];

    //用户所在节点
    var nodeArr = [];

    //获取参数信息
    var bodyParams = req.routeInfo.bodyParams;

    //任务流创建
    async.waterfall([

        //查询登录用户信息
        (cb) => {

            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_username/" + bodyParams.u_username + "/u_is_delete/0/u_is_activation/1",
                    method: 'POST',
                    data: {
                        fields: [
                            "u_id",
                            "u_username",
                            "u_password",
                            "u_nickname"
                        ].join(",")
                    }
                },
                function (data) {

                    userData = data.result.data;

                    //判断该用户是否存在
                    if (userData.length <= 0) {
                        return RES.errorResponse(res,"抱歉，该用户不存在");
                    }

                    //查看密码是否正确
                    if (userData[0].u_password != bodyParams.u_password) {
                        return RES.errorResponse(res,"抱歉，密码输入错误");
                    }

                    //去除密码项
                    delete (userData[0]['u_password']);

                    //返回用户基本信息
                    resultData.userInfo = userData[0];

                    cb();

                }
            )

        },

        //获取用户所在节点
        (cb) => {

            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.userOrganizationalMiddleSearch.Request_Api_Url + "/uom_uid/" + userData[0].u_id + "/skip/0/limit/1000",
                    method: 'POST',
                    data: {
                        fields: "uom_osid"
                    }
                },
                function (data) {

                    nodeArr = data.result.data

                    cb();

                }
            )
        },

        //获取节点所拥有的角色
        (cb) => {

            //如果没有节点直接跳过
            if (nodeArr.length <= 0) {
                return cb()
            }

            //拼接in语句用请求参数
            var inStr = "";
            nodeArr.forEach((item) => {
                inStr += item.uom_osid + ","
            })

            inStr = common.takeStrLast(inStr)

            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.organizationalRoleMiddleSearch.Request_Api_Url + "/in/orm_osid-" + inStr + "/skip/0/limit/1000",
                    method: 'POST',
                    data: {
                        fields: "orm_orid"
                    }
                },
                function (data) {

                    organizationRoleArr = data.result.data

                    cb()

                }
            )


        },

        //获取用户所拥有的角色
        (cb) => {

            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.userUserRoleInfoSearch.Request_Api_Url + "/u_id/" + userData[0].u_id + "/skip/0/limit/1000",
                    method: 'POST',
                    data: {
                        fields: "or_id,or_is_all"
                    }
                },
                function (data) {

                    //用户所拥有的角色
                    userRoleArr = data.result.data;

                    cb();

                }
            )

        },

        //判断是否为超管，如果为超管直接获取全部菜单返回
        (cb) => {

            if (organizationRoleArr.length <= 0 && userRoleArr.length <= 0) {
                return RES.errorResponse(res,"抱歉，该用户无任何权限，无法进行登录使用");
            }

            //判断是否为超管，如果为超管跳过权限组合操作
            var isRoot = false;

            userRoleArr.filter((item) => {
                if (item.or_is_all == 1) {
                    isRoot = true;
                    return;
                }
            });

            //设置为非超级管理员
            resultData.userInfo.isAdmin = false;

            //直接获取权限菜单，以及菜单对应的操作
            if (isRoot) {

                //设置为超管
                resultData.userInfo.isAdmin = true;

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url: CORE_APIConfig.coreAPI.menuSearch.Request_Api_Url + "/m_is_delete/0/skip/0/limit/1000",
                        method: 'GET'
                    },
                    function (data) {

                        //角色所拥有的菜单
                        resultData.menuData = data.result.data;

                        //返回最终结果
                        return RES.successResponse(res,resultData);

                    }
                )

            } else {

                cb()

            }



        },

        //组合节点角色和用户角色，确定最终所拥有的角色
        (cb) => {

            console.log(666);

            var orArr = [];

            for(var i=0;i<organizationRoleArr.length;i++){
                orArr.push(organizationRoleArr[i].orm_orid)
            }

            for(var i=0;i<userRoleArr.length;i++){
                orArr.push(userRoleArr[i].or_id)
            }

            //查询角色所拥有的菜单
            var inStr = orArr.join(",");

            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.roleRoleMenuInfoSearch.Request_Api_Url + "/in/or_id-" +inStr+ "/skip/0/limit/1000",
                    method: 'POST',
                    data: {
                        fields: [
                            "m_id",
                            "m_name",
                            "m_url",
                            "m_icon_type",
                            "m_icon",
                            "m_level",
                            "m_fid",
                            "m_type",
                            "m_path",
                            "m_route_url",
                            "m_is_show",
                            "rmm_apids"
                        ].join(",")
                    }
                },
                function (data) {

                    //角色所拥有的菜单
                    resultData.menuData = data.result.data;

                    //数组去重
                    resultData.menuData = common.ObjectArrDistinct(resultData.menuData,"m_id");

                    cb();

                }
            )

        }

    ], () => {

        //返回最终拼接结果
        RES.successResponse(res,resultData);

    });

}